package com.paddyi.commons.http;

import com.paddyi.commons.utils.StringUtils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @program: paddyi
 * @description: 获取authHeader
 * @author: paddy
 * @create: 2019-05-09 22:40
 **/
public class AuthHeader {
    private String jwtToken = "" ;
    public static String getAuthHeader(HttpServletRequest request,String header) {
        AuthHeader authHeader = new AuthHeader();
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookie.getName().equals(header)) {
                    authHeader.setJwtToken(cookie.getValue());
                    break;
                }
            }
        }
        if (StringUtils.isBlank(authHeader.getJwtToken()) && StringUtils.isNotBlank(request.getHeader(header))) {
            authHeader.setJwtToken(request.getHeader(header)); //获取浏览器传过来的token信息
        }
        return authHeader.getJwtToken();
    }

    public String getJwtToken() {
        return jwtToken;
    }

    public void setJwtToken(String jwtToken) {
        this.jwtToken = jwtToken;
    }
}
